home *** CD-ROM | disk | FTP | other *** search
- Path: isonews.bbn.hp.com!hpbblb!news
- From: Matthias Dittrich <matti>
- Newsgroups: comp.lang.c
- Subject: Re: Permutations
- Date: 29 Feb 1996 18:30:15 GMT
- Organization: Hewlett-Packard Co.
- Message-ID: <4h4rbn$7es@hpbblb.bbn.hp.com>
- References: <Dn8yz9.GGy@spuddy.mew.co.uk> <4h1v27$cdc@texas.nwlink.com>
- NNTP-Posting-Host: trabant.bbn.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
- X-URL: news:4h1v27$cdc@texas.nwlink.com
-
- Teresa Reiko <tjr19@mail.nwlink.com> wrote:
- >david@spuddy.mew.co.uk (David Turner) wrote:
- >
- >> Does anyone know of an algorithm that works out the different permutations
- >>of ordering a number of objects? Not calculating how many there are, but
- >>actaully working out (quickly?) what they permutations are!
- >
- >Untested code -- but this should work...
- >Example: Permute("", "abcdef", 6)
- >
- >void Permute(char *p_prefix, char *p_list, int num)
- >{
- > int i;
- > char s[10], p[10];
- >
- > if(num == 0)
- > {
- > printf("%s%s", prefix, p_list);
- Here is a little bug: ^^^^^^
- you mean p_prefix I think.
-
- (And the output is more readable if you append \n to the format string :-)
- > return;
- > }
- >
- > for(i = 0; i < num; i++)
- > {
- > s[1] = 0;
- > s[0] = p_list[i];
- > strcpy(p, p_prefix);
- > strcat(p, s);
- >
- > strncpy(s, p_list, i);
- > s[i] = 0;
- > strcat(s, p_list + i + 1);
- >
- > Permute(p, s, num - 1);
- > }
- >}
- >...
-
-